(function($) {
    // 编辑器
    var editor;
    KindEditor.ready(function(K) {
        editor = K.create('.business-mark-textarea', {
            width: '700px',
            height: '200px',
            items: [
                'source',
                'fontname',
                'fontsize',
                '|',
                'forecolor',
                'hilitecolor',
                'bold',
                'italic',
                'underline',
                'removeformat',
                '|',
                'justifyleft',
                'justifycenter',
                'justifyright',
                'insertorderedlist',
                'insertunorderedlist',
                '|',
                'emoticons',
                'link',
                'fullscreen'
            ],
            afterBlur: function() {
                this.sync();
            }
        });
    });

    // 表单验证
    $("#signupForm").validate({
        rules: {
            phone: { required: true, isMobile: true },
            role: "required",
            otherSideUsername: {
                required: true,
                remote: {
                    url: "/user/checkUserName",
                    type: "post", //数据发送方式
                    dataType: "json", //接受数据格式   
                    data: { //要传递的数据
                        username: function() {
                            return $("#otherSideUsername").val();
                        }
                    }
                }
            },
            coinType: {
                required: true
            },
            coinPrice: {
                required: true,
                min: 10,
                number: true
            },
            coinCount: {
                required: true,
                min: 1,
                number: true
            },
            realName: {
                required: true
            },
            agree: "required"
        },
        messages: {
            phone: { required: "当前用户手机号不能为空", isMobile: "当前用户手机号不合法" },
            role: "当前用户交易身份不能为空",
            otherSideUsername: {
                required: "对方用户名不能为空",
                remote: "对方用户名不存在"
            },
            coinType: {
                required: "交易货币类型不能为空"
            },
            coinPrice: {
                required: "交易货币价格不能为空",
                min: "交易货币价格不得低于10元",
                number: "交易货币价格必须是数字"
            },
            coinCount: {
                required: "交易货币数量不能为空",
                min: "交易货币数量不能小于1",
                number: "交易货币数量必须是数字"
            },
            realName: {
                required: "买家真是姓名不能为空"
            },
            agree: "用户必须遵守我们的协议"
        },
        submitHandler: function(form) {
            // form.submit();
            var data = $(form).serializeArray();
            $.ajax({
                url: "save",
                data: data,
                method: 'post',
                dataType: 'json',
                success: function(data) {
                    console.log(data);
                    if (data > 0) {
                        window.location.href = "index";
                    } else {
                        alert("添加交易失败,请稍后再试...");
                    }
                }
            });

            return false;
        }
    });

})(jQuery);

/**
 * 购买/卖出 货币的价格和数量 改变时.或中介费支付方式改变时,卖方跟买方所付资金的改变监听
 * @param {*中介费所占比例} agencyFeePayPercent 需要除以100 在使用
 */
function priceChanged(agencyFeePayPercent, payType) {

    var price = $("#coinPrice").val();
    var count = $("#coinCount").val();
    if (payType == undefined) {
        payType = $('input[name="agency_fee_pay_type"]:checked').val();
    }

    var orderAmount = 0;
    var saleGains = 0;

    if (payType == 1) {
        orderAmount = price * count + price * count * agencyFeePayPercent / 100.0;
        saleGains = price * count;
    } else if (payType == 2) {
        saleGains = price * count - price * count * agencyFeePayPercent / 100.0;
        orderAmount = price * count;
    } else if (payType == 3) {
        orderAmount = price * count + price * count * agencyFeePayPercent / 100.0 / 2;
        saleGains = price * count - price * count * agencyFeePayPercent / 100.0 / 2;
    } else if (payType == 4) {
        orderAmount = price * count + price * count * agencyFeePayPercent / 100.0;
        saleGains = price * count - price * count * agencyFeePayPercent / 100.0;
    }

    $("#order_amount").text(orderAmount + "元");
    $("#sale_gains").text(saleGains + "元");
}